// pages/class/index.js

import {goifOpen,goCollection,readliang} from '../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    className:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const vm = this;
    //设置的当前页面标题
    if(options.className){
      wx.setNavigationBarTitle({title: options.className})
    }else{
      options.className = '标题为空'
    }
    

    if(options.id){
      wx.getStorage({
        key:'userInfo',
        success:function(e){
          wx.request({
            url: 'https://www.bchzh.xin/hzmobilework/app/fileClassification/getClassification',
            // url: 'http://192.168.0.30:8080/hzmobilework/app/fileClassification/getClassification',
            data: {parent_id:options.id,userId:e.data.id},
            header: {'content-type': 'application/json'},
            success(res) {
              // console.log(res.data.data.list);
              vm.setData({
                className:res.data.data.list
              })
            }
          })
        }
      })
    }

  },
  godetails(e){
    var data = e.currentTarget.dataset.da;
    // console.log(data)
    // return;
    //type   0视频  1  文档   2  目录
    if(data.type == '2'){
      readliang(data.id,()=>{
        wx.navigateTo({ url: '/pages/class/index?className=' + data.title + '&id=' + data.id })
      })
    }else if((data.type == '1')||(data.type == '0')){
      readliang(data.id,()=>{
        goifOpen(data)
      })
    }
  },
  onSearch(){
    if(this.data.value){
      wx.navigateTo({url: '/pages/search/index?key='+this.data.value})
    }else{
      wx.showToast({title:'关键字为空',icon:'none'})
    }
  },
  collection(e){
    var data = e.currentTarget.dataset.da;
    const vm = this;
    if(data.iscollection!="1"){
      goCollection(data.id,()=>{
        vm.data.className.forEach(element => {
          if(element.id == data.id){
            element.iscollection = '1'
          }  
        })
        
        vm.setData({
          className:vm.data.className
        })
      });
    }
  },
  onChange(event){
    this.setData({
      value: event.detail
    });
  }
})